package main.cs4295_asg2_cameraapps;

import java.io.File;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
	Activity self = this;
	Button btnEditImageView;
	private static final int TAKE_PICTURE = 0;
	private static final int LOAD_PICTURE = 1;
	private Uri mUri;
	private Bitmap photoFromCamera;
	private Drawable imageFromBitmap;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		((Button) findViewById(R.id.snapButton)).setOnClickListener(this);
		((Button) findViewById(R.id.loadButton)).setOnClickListener(this);
		((Button) findViewById(R.id.btnEditImage)).setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.snapButton) {
			Intent photoIntent = new Intent(
					"android.media.action.IMAGE_CAPTURE");
			File storedImage = new File(
					Environment.getExternalStorageDirectory(), "photo.jpg");
			photoIntent.putExtra(MediaStore.EXTRA_OUTPUT,
					Uri.fromFile(storedImage));
			mUri = Uri.fromFile(storedImage);
			startActivityForResult(photoIntent, TAKE_PICTURE);
		} else if (v.getId() == R.id.loadButton) {
			Intent photoIntent = new Intent(
					Intent.ACTION_PICK,
					android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
			startActivityForResult(photoIntent, LOAD_PICTURE);
		} else if (v.getId() == R.id.btnEditImage) {
			Intent switchAct = new Intent(self, editImage.EditImageMain.class);
			switchAct.putExtra("photoFromCamera", photoFromCamera);
			self.startActivity(switchAct);

		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Activity.RESULT_OK && data != null) {
			switch (requestCode) {
			case LOAD_PICTURE:
				mUri = data.getData();
			case TAKE_PICTURE:
				getContentResolver().notifyChange(mUri, null);
				ContentResolver cr = getContentResolver();
				try {
					photoFromCamera = android.provider.MediaStore.Images.Media
							.getBitmap(cr, mUri);
					if (photoFromCamera != null) {
						imageFromBitmap = (new BitmapDrawable(getResources(),
								photoFromCamera));
					}
				} catch (Exception e) {
					Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT)
							.show();
				}
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
